<?php
class ControllerModuleProductLeft extends Controller {
    protected function index() {
        $this->language->load('module/product');

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->load->model('catalog/product');        
        $this->load->model('catalog/discount'); 
        $this->load->model('tool/seo_url');
        $this->load->model('tool/image');

        $this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');
        $this->data['template'] = $this->config->get('config_template');

        $limit = $this->isMobileEnabled() ? 4 : 0;
        $this->data['products'] = array(); 
        $results = $this->model_catalog_product->getSpecialLeftProducts($limit);
		
        foreach ($results as $result) {
			if($result['product_id']) {
				if ($result['image']) {
					$image = $result['image'];
				}
				else {
					$image = 'no_image.jpg';
				}
					
				/*
				 $special = FALSE;
				$discount = $this->model_catalog_product->getProductDiscount($result['product_id']);
				if ($discount) {
				$price = $this->currency->formatPrice($this->tax->calculate($discount, $result['tax_class_id'], $this->config->get('config_tax')));
				}
				else {
				$price = $this->currency->formatPrice($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
				$special = $this->model_catalog_product->getProductSpecial($result['product_id']);
				if ($special) {
				$special = $this->currency->formatPrice($this->tax->calculate($special, $result['tax_class_id'], $this->config->get('config_tax')));
				}
				}
				*/
				
				$price = $this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'));
				$price_formatted = $this->currency->formatPrice($price);
				$discount = 0;
				$discount_fomatted = 0;
				$discount_data = $this->model_catalog_discount->getDiscount($result['product_id']);
				if($discount_data > 0){
					$discount = $discount_data;
					$discount_fomatted =$this->currency->formatPrice($this->tax->calculate($discount_data, $result['tax_class_id'], $this->config->get('config_tax')));
				}
				
				$discounts_data = $this->model_catalog_product->getProductDiscounts($result['product_id']);
				if($discounts_data){
					$discount = $discounts_data[0]['price'];
					$discount_fomatted = $this->currency->formatPrice($this->tax->calculate($discounts_data[0]['price'], $result['tax_class_id'], $this->config->get('config_tax')));
				}
				$discounts = array();
				foreach ($discounts_data as $discount_data) {
					$discounts[] = array(
							'quantity' => $discount_data['quantity'],
							'price'    => $this->currency->formatPrice($this->tax->calculate($discount_data['price'], $result['tax_class_id'], $this->config->get('config_tax')))
					);
				}
				
				$special = $this->model_catalog_product->getProductSpecial($result['product_id']);
				if ($special) {
					$this->data['special'] = $this->currency->formatPrice($this->tax->calculate($special, $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$this->data['special'] = FALSE;
				}
				 
				$attribute_price = 0;
				$min_price = false;
				$price_ref = $discount ? $discount : $price;
				//$options = $this->model_catalog_product->getProductOptions($result['product_id']);
				$options = $this->model_catalog_product->getActiveProductOptions($result['product_id']);
				foreach($options as $option_item){
					foreach($option_item['option_value'] as $option_value){
						$option_price = (float)$option_value['price'] ? $this->tax->calculate($option_value['price'], $result['tax_class_id'], $this->config->get('config_tax')) : 0;
						if($option_price && ($option_value['prefix'] == '-')){
							$option_price = -1 * $option_price;
						}
				
						if($option_price < $attribute_price){
							$attribute_price = $option_price;
						}
				
						if($option_price > 0){
							$min_price = true;
						}
					}
				}
				 
				$add = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id']);
					
				$colors = array();
				/*
				 $product_colors = $this->model_catalog_product->getProductColors($result['product_id']);
				foreach ($product_colors as $item) {
				$image_list = $this->model_catalog_product->getProductColorImages($item['product_id'], $item['product_color_id']);
				if(!empty($image_list)){
				$colors[] = array(
						'full'	 	 => HTTPS_IMAGE . $item['image'],
						//'popup' 	 => $this->model_tool_image->resize($item['image'], 291, 385),
						'thumb' 	 => $this->model_tool_image->resizeNoScale($item['color_image'], 17, 8),
				);
				}
				}
				*/
				//echo $price_ref . ":";
				//echo $price;
				//exit;
				$rate_discount = ((floatval($price) - floatval($price_ref)) / floatval($price)) * 100;
				
				$rate_discount = round($rate_discount);
					
				if($attribute_price != 0){
					$ref_price =  $this->currency->formatPrice($price_ref + $attribute_price);
				}
				elseif($min_price){
					$ref_price = $this->currency->formatPrice($price_ref);
				}
				else{
					$ref_price = 0;
				}
				
				
				
				//$shortname = (strlen($result['name']) <= 16) ? $result['name'] : substr($result['name'], 0 ,16) . ' ...';
				$shortname = $this->model_catalog_product->breakProductName($result['name'], 34);
				
				$path = $this->model_catalog_product->getCategoriesPath($result['product_id']);
				$href = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&path=' . $path . '&product_id=' . $result['product_id']);
				
				$this->data['products'][] = array(
						'name'      => $result['name'],
						'product_id'=> $result['product_id'],
						'shortname' => $shortname,
						//'model'     => $result['model'],
						//'thumb'     => $this->model_tool_image->resize($image, 121, 148),
						'thumb'     => $this->model_tool_image->resize($image, 188, 228),
						'price'     => $price_formatted,
						'ref_price' => $ref_price,
						'discount'  => $discount_fomatted,
						'discounts' => $discounts,
						'options'   => $options,
						'special'   => $special,
						'href'      => $href,
						'add'	    => $add,
						'rate_discount' => $rate_discount,
						//'sku'       => $result['sku'],
						'colors'	=> $colors,
						'unbreak'   => mb_strlen($shortname, 'UTF-8') < 34,
				);
			}
			$this->load->model('setting/tooltip');
			$this->data['shipping_note'] = $this->model_setting_tooltip->getShippingNote();
        }
         
        $this->id = 'product_left';
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/product_left.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/product_left.tpl';
        } else {
            $this->template = 'default/template/module/product.tpl';
        }
        
        $this->render();
    }
}
?>
